package com.zj.album.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.text.TextUtils;
import android.widget.ImageView;

import com.zj.album.R;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import static com.squareup.picasso.MemoryPolicy.NO_CACHE;
import static com.squareup.picasso.MemoryPolicy.NO_STORE;

/**
 * Created by zhaojie on 2017/10/10.
 */

public class ImageLoaderUtils {

    public static ImageView load(Context context, ImageView iv, String url, int width, int height) {
        width = calculateOptions(width);
        height = calculateOptions(height);
        Picasso.with(context).load(TextUtils.isEmpty(url) ? "0" : url).memoryPolicy(NO_CACHE, NO_STORE).centerCrop().resize(width, height).placeholder(R.drawable.photo_default_bg).error(R.drawable.photo_default_bg).into(iv);
        return iv;
    }

    public static ImageView load(Context context, ImageView iv, String url, int width, int height, int defaultBg) {
        width = calculateOptions(width);
        height = calculateOptions(height);
        Picasso.with(context).load(TextUtils.isEmpty(url) ? "0" : url).centerCrop().resize(width, height).placeholder(defaultBg).error(defaultBg).into(iv);
        return iv;
    }

    public static ImageView loadLocalBitmap(Context context, ImageView iv, String uri, int w, int h) {
        int degree = readPicDegree(uri);
        Picasso.with(context).load(TextUtils.isEmpty(uri) ? "0" : uri).memoryPolicy(NO_CACHE, NO_STORE).rotate(degree).centerCrop().resize(w, h).error(R.drawable.photo_default_bg).into(iv);
        return iv;
    }

    /**
     * 通过ExifInterface类读取图片文件的被旋转角度
     * 解决系统将保存的图片自动旋转的问题
     *
     * @param path ： 图片文件的路径
     * @return 图片文件的被旋转角度
     */
    public static int readPicDegree(String path) {
        if (TextUtils.isEmpty(path)) return 0;
        int degree = 0;
        // 读取图片文件信息的类ExifInterface
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(path);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (exif != null) {
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
            if (orientation != -1) {
                switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        degree = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        degree = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        degree = 270;
                        break;
                }
            }
        }
        return degree;
    }

    private static int calculateOptions(int ops) {
        return ops <= 0 ? 10 : ops;
    }

    //将Drawable转为bitmap
    public static Bitmap drawableToBitmap(Drawable drawable, int w, int h, boolean isNeedClip) {
        int width = isNeedClip ? w : drawable.getIntrinsicWidth();   // 取 drawable 的长宽
        int height = isNeedClip ? h : drawable.getIntrinsicHeight();
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;         // 取 drawable 的颜色格式
        Bitmap bitmap = Bitmap.createBitmap(width, height, config);     // 建立对应 bitmap
        Canvas canvas = new Canvas(bitmap);         // 建立对应 bitmap 的画布
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);      // 把 drawable 内容画到画布中
        return bitmap;
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        return drawableToBitmap(drawable, 0, 0, false);
    }
}
